package com.ccx.coachsite.api;


import java.util.ArrayList;

import android.content.Context;

import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.data.SystemConst;
import com.ccx.coachsite.downloadmgr.DownloadService;
import com.ccx.coachsite.downloadmgr.ImageCacheTool;
import com.ccx.coachsite.model.CSResultCodeMap;
import com.ccx.coachsite.model.PageInfo;
import com.ccx.coachsite.model.ServerDetailResult;
import com.ccx.coachsite.model.ServerResult;
import com.ccx.coachsite.model.SoftCategoryItem;
import com.ccx.coachsite.model.SoftKeywordItem;
import com.ccx.coachsite.model.SoftShareBySmItem;
import com.ccx.coachsite.model.SoftShareBySmsDetailItem;
import com.ccx.coachsite.model.SoftShareMessage;
import com.ccx.coachsite.model.SoftTopicItem;
import com.ccx.coachsite.model.SoftVersionUpdateItem;
import com.ccx.coachsite.ui.login.LoginHelper;
import com.ccx.coachsite.ui.softmgr.SoftInfo;
import com.ccx.coachsite.util.FileUtil;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;
import com.ccx.coachsite.weibo.json.JSONArray;
import com.ccx.coachsite.weibo.json.JSONException;
import com.ccx.coachsite.weibo.json.JSONObject;

/**
 * 应用访问
 * @author cfb
 *
 */
public class CoachSiteSoftMgrApiAccessor {

	private static final String  TAG = "CoachSiteSoftMgrApiAccessor";
	
	public static final String PAGE_NUMBER = "pageNumber";
	public static final String USER_ID = "userId";
	public static final String SOFT_ID = "id";
	
	/*应用模块 Begin*/
	/**专题列表*/
	private static final String  API_SOFTMGR_TOPIC_LIST= CoachSiteApiAccessor.BASE_URL+"/3g/topicCategory/list";
		
	/**专题下应用列表*/
	public static final String  API_SOFTMGR_TOPIC_APP_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/topic/item/list/";
	
	/**获取专题明细*/
	public static final String  API_SOFTMGR_TOPIC_DETAIL = CoachSiteApiAccessor.BASE_URL+"/3g/project/detail/";
	
	/**应用类型列表*/
	public static final String  API_SOFTMGR_TYPE_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/appCategory/list";
	
	/**分类 应用列表*/
	public static final String  API_SOFTMGR_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/app/item/list/";

	/**游戏类型列表*/
	public static final String  API_SOFTMGR_GAME_TYPE_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/game/list";
	
	/**分类游戏列表*/
	public static final String  API_SOFTMGR_GAME_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/app/game/list/";
	
	/**应用详细*/
	private static final String  API_SOFTMGR_DETAIL = CoachSiteApiAccessor.BASE_URL+"/3g/app/item/detail/";

	/**应用统计*/
	private static final String  API_SOFTMGR_DOWN_COUNT = CoachSiteApiAccessor.BASE_URL+"/3g/app/account";

	/**添加收藏*/
	private static final String API_SOFTMGR_COLLECT = CoachSiteApiAccessor.BASE_URL+"/3g/app/collect";
	
	/**取消收藏*/
	private static final String API_SOFTMGR_COLLECT_CANCLE = CoachSiteApiAccessor.BASE_URL+"/3g/Dele/collectApp";
			
	/**搜索关键字*/
	private static final String API_SOFTMGR_SEARCH_KEYWORDS = CoachSiteApiAccessor.BASE_URL+"/3g/app/search/keywords";
	
	/**根据关键字获取应用列表*/
	public static final String API_SOFTMGR_SEARCH_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/search/item/list";
		
	/**根据名字获取应用列表*/
	public static final String API_SOFTMGR_SEARCH_BY_NAME_LIST = CoachSiteApiAccessor.BASE_URL+"/3g/search/appname/list";
	
	/**获取达人推荐应用列表*/
	private static final String API_SOFTMGR_SEARCH_TALENT_RECOMMEND = CoachSiteApiAccessor.BASE_URL+"/3g/app/talent/recommend/list";

	/**软件升级接口*/
	private static final String API_SOFTMGR_VERSION_UPDATE = CoachSiteApiAccessor.BASE_URL+"/3g/check/version";

	/**根据编号获取认领用户列表*/
	private static final String API_SOFTMGR_SHARE_USERS = CoachSiteApiAccessor.BASE_URL+"/3g/invited/queryFriends";

	/**一键分享应用给好友*/
	public static final String API_SOFTMGR_SHARE_TO_USERS = CoachSiteApiAccessor.BASE_URL+"/3g/app/share";
	
	/**一键分享游戏给好友*/
	public static final String API_GAME_SHARE_TO_USERS = CoachSiteApiAccessor.BASE_URL+"/3g/game/share";
	
	/**一键分享教程给好友*/
	public static final String API_TUTORIAL_SHARE_TO_USERS = CoachSiteApiAccessor.BASE_URL+"/3g/tutorial/share";
	
	/**一键分享专题给好友*/
	public static final String API_TOPIC_SHARE_TO_USERS = CoachSiteApiAccessor.BASE_URL+"/3g/project/share";

	/**获取分享数据*/
	private static final String API_GET_SHAREMESSAGE = CoachSiteApiAccessor.BASE_URL+"/3g/share/download";

	/**短信分享教程和应用*/
	public static final String API_SMS_SHARE = CoachSiteApiAccessor.BASE_URL+"/3g/user/tutoshare";

	/**统计客户端分享的应用教程及专题*/
	private static final String API_SMS_SHARE_COUNT = CoachSiteApiAccessor.BASE_URL+"/3g/share/download/statistics";

	
	/*应用模块 End*/
	
	/**
	 * 获取应用类型列表
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftCategoryItem> getSoftCategoryList(String reqUrl, int pageNumber){
		
		ServerResult<SoftCategoryItem> softCategoryResult = new ServerResult<SoftCategoryItem>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(reqUrl+"?"+PAGE_NUMBER+"="+pageNumber+"&pageSize=15", "utf-8");
		if( null == jsonString ) {
			softCategoryResult.setbNetworkProblem(true);
			return softCategoryResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCategoryResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				setResultPageInfo(responseObj, softCategoryResult.getPageInfo());
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					softCategoryResult.itemList.add(jsonToSoftCategory(item));
				}
			}
		}catch(Exception ex){
			softCategoryResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softCategoryResult;
	}
	
	/**
	 * json转为分类对象
	 * @return
	 */
	private static SoftCategoryItem jsonToSoftCategory(JSONObject json) throws JSONException{
		
		SoftCategoryItem softCategory = new SoftCategoryItem();
		softCategory.setId(json.getInt("id"));
		softCategory.setName(json.getString("name"));
		
		return softCategory;
	}
	
	/**
	 * 通过分类ID 获取应用列表
	 * 
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftInfo> getSoftAppList(String requestURL){
		
		ServerResult<SoftInfo> softListResult = new ServerResult<SoftInfo>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(requestURL, "utf-8");
		if( null == jsonString ) {
			softListResult.setbNetworkProblem(true);
			return softListResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softListResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				setResultPageInfo(responseObj, softListResult.getPageInfo());
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					softListResult.itemList.add(jsonToSoftInfoForList(item));
				}
			}
		}catch(Exception ex){
			softListResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softListResult;
	}
	
	private static SoftInfo jsonToSoftInfoForList(JSONObject json) throws JSONException{
		
		SoftInfo softInfo=new SoftInfo();
		softInfo.setSid(json.getString("id"));
		softInfo.setPkgName(json.getString("packageName"));
		softInfo.setAvailVersion(json.getString("versionName"));		
		softInfo.setTitle(json.getString("name"));
		softInfo.setIconUrl(json.getString("iconUrl"));
		softInfo.setDownloadUrl(json.getString("apkUrl"));
		//softInfo.setSize(json.getInt("size")+""); //文件大小，带KB，MB字串
		softInfo.setFileBytes(json.getInt("size"));
		softInfo.setGrade(json.getInt("rating"));
		softInfo.setFlag(json.getString("flag"));
		softInfo.setFilePath(CoachSiteApiAccessor.getFilePath(softInfo));
		int activeStatus=DownloadService.getDownloadStatus(CoachSiteApiAccessor.getAppContext(), softInfo);//当前该的状态
		softInfo.setStatus(activeStatus);
		
		return softInfo;
	}
	
	/**
	 * 通过分类ID 获取应用列表
	 * @param softID
	 * @param userID
	 * @param flag  应用归属 1为应用汇 2天翼空间
	 * @return
	 */
	public static ServerDetailResult<SoftInfo> getSoftAppDetail(int softID, String userID,String flag){
		
		ServerDetailResult<SoftInfo> softListResult = new ServerDetailResult<SoftInfo>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_DETAIL+softID+"?"+USER_ID+"="+userID+"&flag="+flag, "utf-8");
		if( null == jsonString ) {
			softListResult.setbNetworkProblem(true);
			return softListResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softListResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				softListResult.detailItem = jsonToSoftInfoForList(responseObj);
				
				softListResult.detailItem.setIntroduction(responseObj.getString("description"));//描述
				softListResult.detailItem.setDownloadCount(responseObj.getString("count"));		//下载次数
				softListResult.detailItem.setCategoryName(responseObj.getString("categoryName"));//所属分类
				softListResult.detailItem.setCollection(responseObj.getInt("collection")==0?false:true);//是否收藏				
				softListResult.detailItem.setSdkVersion(responseObj.getString("sdkVersion"));
				
				String iconUrl=softListResult.detailItem.getIconUrl();
				if(iconUrl!=null)
					iconUrl=iconUrl.replaceAll("\\\\", "/");
				//从url路径中抽取文件名
				String iconFileName=FileUtil.getFileNameFromPath(iconUrl, true);
				softListResult.detailItem.setIconPath(SystemConst.DOWNLOAD_DIR+iconFileName);
				
				softListResult.detailItem.preImageUrlsList.addAll(getSnapshotUrls(responseObj.getString("snapshotUrls")));
				/*
				JSONArray snapshotsArray = responseObj.getJSONArray("snapshotUrls");
				for(int i=0;i<snapshotsArray.length();i++){
					JSONObject item =  snapshotsArray.getJSONObject(i);
					softListResult.detailItem.preImageUrlsList.add(item.getString("url")); 
				}
				*/
			}
		}catch(Exception ex){
			softListResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softListResult;
	}
	
	public static ArrayList<String> getSnapshotUrls(String strSnapshotUrls){
		
		ArrayList<String> result =  new ArrayList<String>();
		
		if ( strSnapshotUrls!=null && !"".equals(strSnapshotUrls) ){
			
			String[] strArray=strSnapshotUrls.split(","); 
			if ( strArray!=null ) {
				for (int i=0; i<strArray.length; i++) {
					result.add(strArray[i]);
				}
			}
		}
		
		return result;
	}
	
	/**
	 * 获取专题列表
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftTopicItem> getSoftMgrTopicList(int pageNumber){
		
		ServerResult<SoftTopicItem> softTopicResult = new ServerResult<SoftTopicItem>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_TOPIC_LIST+"?"+PAGE_NUMBER+"="+pageNumber+"&pageSize=15", "utf-8");
		if( null == jsonString ) {
			softTopicResult.setbNetworkProblem(true);
			return softTopicResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softTopicResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				setResultPageInfo(responseObj, softTopicResult.getPageInfo());
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					softTopicResult.itemList.add(jsonToSoftTopic(item));
				}
			}
		}catch(Exception ex){
			softTopicResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softTopicResult;
	}
	
	
	/**
	 * 通过专题ID 获取专题信息
	 * 
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerDetailResult<SoftTopicItem> getSoftMgrTopicDetail(String topicID){
		
		ServerDetailResult<SoftTopicItem> softListResult = new ServerDetailResult<SoftTopicItem>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_TOPIC_DETAIL+topicID, "utf-8");
		if( null == jsonString ) {
			softListResult.setbNetworkProblem(true);
			return softListResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softListResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				softListResult.detailItem = jsonToSoftTopicDetail(responseObj);
			}
		}catch(Exception ex){
			softListResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softListResult;
	}
	
	/**
	 * json转为专题对象
	 * @return
	 */
	private static SoftTopicItem jsonToSoftTopic(JSONObject json) throws JSONException{
		
		SoftTopicItem softTopic=new SoftTopicItem();
		softTopic.setId(json.getString("id"));
		softTopic.setName(json.getString("name"));
		softTopic.setDesc(json.getString("desc"));
		softTopic.setTypeUrl( ImageCacheTool.encodeImgUrl(json.getString("typeUrl")) );
		softTopic.setAppCount(json.getInt("appCount"));
		softTopic.setDetails(json.getString("details"));
		
		return softTopic;
	}
	
	/**
	 * json转为专题对象
	 * @return
	 */
	private static SoftTopicItem jsonToSoftTopicDetail(JSONObject json) throws JSONException{
		
		SoftTopicItem softTopic=new SoftTopicItem();
		softTopic.setId(json.getString("id"));
		softTopic.setName(json.getString("name"));
		softTopic.setTypeUrl(json.getString("typeUrl"));
		softTopic.setDetails(json.getString("guid"));
		
		return softTopic;
	}
	
	private static void setResultPageInfo(JSONObject responseObj, PageInfo pageInfo) throws JSONException{
		pageInfo.currentPageNum = responseObj.getInt("pageNumber");
		pageInfo.totalPages = responseObj.getInt("totalPages");
		pageInfo.totalRecordNums = responseObj.getInt("totalItems");
	}
	
	/**
	 * 应用收藏
	 * @param softID
	 * @param userID
	 * @return
	 */
	public static ServerDetailResult<Boolean> collectionApp(int softID, String userID){
		
		ServerDetailResult<Boolean> softCollectResult = new ServerDetailResult<Boolean>();
		softCollectResult.detailItem = false;
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_COLLECT+"?"+SOFT_ID+"="+softID+"&"+USER_ID+"="+userID, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				softCollectResult.detailItem = true;
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}
	
	/**
	 * 应用取消收藏
	 * @param softID
	 * @param userID
	 * @return
	 */
	public static ServerDetailResult<Boolean> collectionCancleApp(int softID, String userID){
		
		ServerDetailResult<Boolean> softCollectResult = new ServerDetailResult<Boolean>();
		softCollectResult.detailItem = false;
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_COLLECT_CANCLE+"?"+SOFT_ID+"="+softID+"&"+USER_ID+"="+userID, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				softCollectResult.detailItem = true;
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}
	
	/**
	 * 获取关键字列表
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftKeywordItem> getSoftMgrKeywordList(){
		
		ServerResult<SoftKeywordItem> softKeywordResult = new ServerResult<SoftKeywordItem>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_SEARCH_KEYWORDS, "utf-8");
		if( null == jsonString ) {
			softKeywordResult.setbNetworkProblem(true);
			return softKeywordResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softKeywordResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				
				JSONArray listJson = jsonData.getJSONArray("response");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					softKeywordResult.itemList.add(jsonToSoftkeyword(item));
				}
			}
		}catch(Exception ex){
			softKeywordResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softKeywordResult;
	}
	
	/**
	 * json转为关键字对象
	 * @return
	 */
	private static SoftKeywordItem jsonToSoftkeyword(JSONObject json) throws JSONException{
		
		SoftKeywordItem softKeyword=new SoftKeywordItem();
		softKeyword.setId(json.getInt("id"));
		softKeyword.setKeyword(json.getString("keyword"));
		softKeyword.setTotal(json.getInt("total"));
		
		return softKeyword;
	}	
	
	/**
	 * 通过关键词 获取应用列表 Post
	 * 
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftInfo> getSoftAppListByKeyWord(String keyName, String keyword, int pageNumber, String baseURL){
		
		ServerResult<SoftInfo> softListResult = new ServerResult<SoftInfo>();
		
		String jsonString = CoachSiteApiAccessor.getURLContentByPost(baseURL, keyName, keyword, pageNumber, "utf-8");
		if( null == jsonString ) {
			softListResult.setbNetworkProblem(true);
			return softListResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softListResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				setResultPageInfo(responseObj, softListResult.getPageInfo());
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					softListResult.itemList.add(jsonToSoftInfoForList(item));
				}
			}
		}catch(Exception ex){
			softListResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softListResult;
	}
	
	
	/**
	 * 应用下载统计
	 * @param softID
	 * @param userID
	 * @return
	 */
	public static ServerDetailResult<Boolean> downAppCount(String softID){
		
		ServerDetailResult<Boolean> softCollectResult = new ServerDetailResult<Boolean>();
		softCollectResult.detailItem = false;
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_DOWN_COUNT+"?appId="+softID, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				softCollectResult.detailItem = true;
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}
	
	/**
	 * 
	 * downAppGameCount 软件点击即下载统计
	 * @param appId
	 * @param flag  0为用户查看1为点击了下载
	 * @param ctx
	 * @return
	 *ServerDetailResult<Boolean>
	 * @exception
	 * @since  1.0.0
	 */
	public static ServerDetailResult<Boolean> downAppGameCount(String appId, String flag,Context ctx){
		
		ServerDetailResult<Boolean> softCollectResult = new ServerDetailResult<Boolean>();
		softCollectResult.detailItem = false;
		String mobile = null, userId = null;
		mobile = SharedPreferencesUtil.getValue(ctx, SharedKey.Shared_Mobile_Key, mobile);
		userId = SharedPreferencesUtil.getValue(ctx, SharedKey.Shared_UserId_Key, userId);
		String url = API_SOFTMGR_DOWN_COUNT+"?appId="+appId+"&flag="+flag+"&IMEI="+LoginHelper.getMobileIMEI(ctx);
		if ( !StringUtil.isEmpty(mobile) ) 
			url = url+"&mobile="+mobile+"&userId="+userId;
		
		String jsonString = CoachSiteApiAccessor.getURLContent(url, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				softCollectResult.detailItem = true;
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}

	/**
	 * 获取达人推荐应用列表
	 * @param pageNumber
	 * @return
	 * @throws JSONException
	 */
	public static ServerResult<SoftInfo> getSoftMgRecommendList(){
		
		ServerResult<SoftInfo> softListResult = new ServerResult<SoftInfo>();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_SEARCH_TALENT_RECOMMEND, "utf-8");
		if( null == jsonString ) {
			softListResult.setbNetworkProblem(true);
			return softListResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softListResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					
					SoftInfo softInfo=new SoftInfo();
					softInfo.setSid(item.getString("id"));
					softInfo.setTitle(item.getString("name"));
					softInfo.setIconUrl(item.getString("iconUrl"));
					softInfo.setDownloadUrl(item.getString("apkUrl"));
					
					softListResult.itemList.add(softInfo);
				}
			}
		}catch(Exception ex){
			softListResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return softListResult;
	}
	
	
	/**
	 * 应用升级检测
	 * @param softID
	 * @param userID
	 * @return
	 */
	public static ServerDetailResult<SoftVersionUpdateItem> checkVersion(){
		
		ServerDetailResult<SoftVersionUpdateItem> softCollectResult = new ServerDetailResult<SoftVersionUpdateItem>();
		softCollectResult.detailItem = new SoftVersionUpdateItem();
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SOFTMGR_VERSION_UPDATE, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				softCollectResult.detailItem.version = responseObj.getString("version");
				softCollectResult.detailItem.url = responseObj.getString("url");
				softCollectResult.detailItem.featureinfo = responseObj.getString("featureinfo");
				softCollectResult.detailItem.isNew = responseObj.getInt("isNew");
				softCollectResult.detailItem.createWhen = responseObj.getString("createWhen");
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}
	

	/**
	 * 一键分享
	 * @param userID
	 * @param toShareUserIds
	 * @param id
	 * @param userType
	 * @param shareType  分享类型: 1.教程 2应用 3游戏 4专题
	 * @return
	 */
	public static ServerDetailResult<Boolean> shareInfoToUsersByPost(String userID,String toShareUserIds,String id, String userType, int shareType, String shareContent, String flag){
		
		ServerDetailResult<Boolean> shareResult = new ServerDetailResult<Boolean>();
		shareResult.detailItem = false;
		
		String pathUrl = "";
		
		switch (shareType) {
		case SoftShareMessage.TYPE_TUTORIAL_SHARE_TYPE:
			pathUrl = CoachSiteSoftMgrApiAccessor.API_TUTORIAL_SHARE_TO_USERS;
			break;
		case SoftShareMessage.TYPE_APP_SHARE_TYPE:
			pathUrl = CoachSiteSoftMgrApiAccessor.API_SOFTMGR_SHARE_TO_USERS;
			break;
		case SoftShareMessage.TYPE_GAME_SHARE_TYPE:
			pathUrl = CoachSiteSoftMgrApiAccessor.API_GAME_SHARE_TO_USERS;
			break;
		case SoftShareMessage.TYPE_TOPIC_SHARE_TYPE:
			pathUrl = CoachSiteSoftMgrApiAccessor.API_TOPIC_SHARE_TO_USERS;
			break;
		default:
			break;
		}
		
		String jsonString = "";
		jsonString = CoachSiteApiAccessor.getURLContentByPostForWeiBo(pathUrl,
				userID, toShareUserIds, id, userType, shareType, shareContent,"utf-8", flag);
			
		if( null == jsonString ) {
			shareResult.setbNetworkProblem(true);
			return shareResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			shareResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				shareResult.detailItem = true;
			}
		}catch(Exception ex){
			shareResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return shareResult;
	}
	
	//API_SMS_SHARE
	/**
	 * 
	 * @param userID
	 * @param toShareUserIds
	 * @param id
	 * @param userType
	 * @param shareType
	 * @param groupId
	 * @param flag  1为分组用户2为手机号码或通讯录
	 * @param appFlag  应用所属(分享应用和游戏时必须传值，分享教程和专题时不需要传值)
	 * @return
	 */
	public static ServerDetailResult<SoftShareBySmItem> shareInfoToUsersBySMS(String userID,String toShareUserIds,
			String id, String userType, int shareType, String groupId, String flag, String appFlag){
		
		ServerDetailResult<SoftShareBySmItem> shareResult = new ServerDetailResult<SoftShareBySmItem>();
		shareResult.detailItem = new SoftShareBySmItem();
		shareResult.detailItem.isSuccessful = false;
		
		String pathUrl = API_SMS_SHARE+"?mobile="+toShareUserIds+"&userId="+userID+"&id="+id+"&type="+shareType+"&groupId="+groupId+"&flag="+flag+"&appFlag="+appFlag;
		
		String jsonString = "";
		jsonString = CoachSiteApiAccessor.getURLContent(pathUrl,"utf-8");
			
		if( null == jsonString ) {
			shareResult.setbNetworkProblem(true);
			return shareResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			shareResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				shareResult.detailItem.isSuccessful = true;
				
				JSONObject responseObj = jsonData.getJSONObject("response");
				JSONArray listJson = responseObj.getJSONArray("successList");
				for(int i=0;i<listJson.length();i++){
					JSONObject json =  listJson.getJSONObject(i);
					SoftShareBySmsDetailItem detailItem = new SoftShareBySmsDetailItem();
					detailItem.mobile = json.getString("mobile");
					detailItem.tip = json.getString("tip");
					shareResult.detailItem.successList.add(detailItem);
				}
				
				listJson = responseObj.getJSONArray("errorList");
				for(int i=0;i<listJson.length();i++){
					JSONObject json =  listJson.getJSONObject(i);
					SoftShareBySmsDetailItem detailItem = new SoftShareBySmsDetailItem();
					detailItem.mobile = json.getString("mobile");
					detailItem.tip = json.getString("tip");
					shareResult.detailItem.errorList.add(detailItem);
				}
			}
		}catch(Exception ex){
			shareResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return shareResult;
	}

	/**
	 * 网络获取
	 * @param userID
	 * @return
	 */
	public static ServerResult<SoftShareMessage> getShareMessage(String mobile){
		
		ServerResult<SoftShareMessage> shareResult = new ServerResult<SoftShareMessage>();

		String jsonString = CoachSiteApiAccessor.getURLContent(API_GET_SHAREMESSAGE+"?mobile="+mobile, "utf-8");
		
		if( null == jsonString ) {
			shareResult.setbNetworkProblem(true);
			return shareResult; //网络异常
		}
		
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			shareResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				JSONObject responseObj = jsonData.getJSONObject("response");
				
				JSONArray listJson = responseObj.getJSONArray("list");
				
				for(int i=0;i<listJson.length();i++){
					JSONObject item =  listJson.getJSONObject(i);
					
					SoftShareMessage softShareMessage = new SoftShareMessage();
					softShareMessage.setContent(item.getString("content"));
					softShareMessage.setCreatedTime(item.getString("createdTime"));
					softShareMessage.setType(item.getString("type"));
					softShareMessage.setTypeId(item.getString("typeId"));
					softShareMessage.setUserId(item.getInt("userId"));
					softShareMessage.setUserMobile(item.getString("userMobile"));
					softShareMessage.setUserName(item.getString("userName"));
					softShareMessage.setId(item.getInt("id"));
					
					shareResult.itemList.add(softShareMessage);
				}
			}
		}catch(Exception ex){
			shareResult.setResultCode(CSResultCodeMap.SOFT_CATEGORY_LIST_FAIL_CODE);
			ex.printStackTrace();
		}
		
		return shareResult;
	}
	
	/**
	 * 推荐信息点击统计
	 * @param softID
	 * @param userID
	 * @return
	 */
	private static ServerDetailResult<Boolean> openShareMsgCount(int msgID){
		
		ServerDetailResult<Boolean> softCollectResult = new ServerDetailResult<Boolean>();
		softCollectResult.detailItem = false;
		
		String jsonString = CoachSiteApiAccessor.getURLContent(API_SMS_SHARE_COUNT+"?id="+msgID, "utf-8");
		if( null == jsonString ) {
			softCollectResult.setbNetworkProblem(true);
			return softCollectResult; //网络异常
		}
	
		try{
			JSONObject jsonData = new JSONObject(jsonString);
			int code = jsonData.getInt("code");
			softCollectResult.setResultCode(code);
			if(code == CSResultCodeMap.APP_SUCCESS_CODE){
				softCollectResult.detailItem = true;
			}
		}catch(Exception ex){
			softCollectResult.setResultCode(CSResultCodeMap.SOFT_TOPIC_FAIL_CODE);
			ex.printStackTrace();
		}
		return softCollectResult;
	}
	
	public static void execShareMsgCount(final int msgID){
		
		new Thread() {
			public void run() {
				openShareMsgCount(msgID);
			};
		}.start();
	}
	
	public static void execOpenAppGameCount(final String sid, final Context mContext){
		
		new Thread() {
			public void run() {
				CoachSiteSoftMgrApiAccessor.downAppGameCount(sid,"0", mContext);
			};
		}.start();
	}
}
